package com.sihai.ehr.web.action.salary;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.SalaryIndividualTax;
import com.sihai.ehr.model.biz.SalaryIndividualTaxRule;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.IndividualTaxService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.Iterator;
import java.util.List;

@Results({
        @Result(name = "main", location = "salary_main.jsp"),
        @Result(name = "add", location = "individual_tax_add.jsp"),
        @Result(name = "edit", location = "individual_tax_edit.jsp"),
        @Result(name = "list", location = "individual_tax_list.jsp")
})
public class IndividualTaxAction extends EHRAction {
    @BeanInject
    private OrganizationService organizationService;
    @BeanInject
    private IndividualTaxService individualTaxService;

    private List<Organization> organizations;
    private LimitedList<SalaryIndividualTax> salaryIndividualTaxes;
    private LimitedList<SalaryIndividualTaxRule> salaryIndividualTaxRules;

    private SalaryIndividualTax salaryIndividualTax;

    private String taxIds;

    @Action("/salary/individual_tax_main")
    public String execute() throws Exception {
        listTitle = "个人所得税管理";
        listAction = "/salary/individual_tax_list.xhtml";
        organizations = organizationService.findChildren(null, true, null, null);
        if (organizations != null && organizations.size() > 0) {
        	if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
            organizationId = organizations.get(0).getId();
        }
        showChildOrg = false;
        return "main";
    }

    @Action("/salary/individual_tax_edit")
    public String add() throws Exception {
        if (salaryIndividualTax != null && salaryIndividualTax.getId() > 0) {
            salaryIndividualTax = individualTaxService.get(salaryIndividualTax.getId());
            return "edit";
        } else {
            return "add";
        }

    }

    @Action("/salary/individual_tax_list")
    public String list() throws Exception {
        salaryIndividualTaxRules= individualTaxService.queryTaxRule(organizationId,createLimit(),createSorter());   // 个税扣款规则
        salaryIndividualTaxes = individualTaxService.query(organizationId, createLimit(), createSorter());

        /* 分页 */
        this.paging(salaryIndividualTaxes);
        this.setPageUrl("salary/individual_tax_list.xhtml");
        return "list";
    }

    @Action("/salary/individual_tax_save")
    public String save() throws Exception {
        if (salaryIndividualTax == null)
            return execute();
        if (salaryIndividualTax.getId() != null && salaryIndividualTax.getId() > 0) {
            SalaryIndividualTax tax = individualTaxService.get(salaryIndividualTax.getId());
            tax.setTitle(salaryIndividualTax.getTitle());
            tax.setCostBase(salaryIndividualTax.getCostBase());
            tax.setLinkedDataType(salaryIndividualTax.getLinkedDataType());
            tax.setRemoveInsurance(salaryIndividualTax.isRemoveInsurance());
            individualTaxService.save(tax);
        } else {
            individualTaxService.save(salaryIndividualTax);
        }
        return execute();
    }

    @Action("/salary/individual_tax_remove")
    public String remove() throws Exception {
        if(StringUtils.isBlank(taxIds)){
            return execute();
        }
        individualTaxService.remove(taxIds);
        return execute();
    }
   
    public List<Organization> getOrganizations() {
        return organizations;
    }

    public void setOrganizations(List<Organization> organizations) {
        this.organizations = organizations;
    }

    public SalaryIndividualTax getSalaryIndividualTax() {
        return salaryIndividualTax;
    }

    public void setSalaryIndividualTax(SalaryIndividualTax salaryIndividualTax) {
        this.salaryIndividualTax = salaryIndividualTax;
    }

    public LimitedList<SalaryIndividualTax> getSalaryIndividualTaxes() {
        return salaryIndividualTaxes;
    }

    public void setSalaryIndividualTaxes(LimitedList<SalaryIndividualTax> salaryIndividualTaxes) {
        this.salaryIndividualTaxes = salaryIndividualTaxes;
    }

    public String getTaxIds() {
        return taxIds;
    }

    public void setTaxIds(String taxIds) {
        this.taxIds = taxIds;
    }

    public LimitedList<SalaryIndividualTaxRule> getSalaryIndividualTaxRules() {
        return salaryIndividualTaxRules;
    }

    public void setSalaryIndividualTaxRules(LimitedList<SalaryIndividualTaxRule> salaryIndividualTaxRules) {
        this.salaryIndividualTaxRules = salaryIndividualTaxRules;
    }
}
